package com.xiaodu.acer.phoneplay.ui.base;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.bean.PageBean;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerAppInfoComponent;
import com.xiaodu.acer.phoneplay.di.module.AppInfoModule;
import com.xiaodu.acer.phoneplay.presenter.AppInfoPresenter;
import com.xiaodu.acer.phoneplay.presenter.contract.AppInfoContract;
import com.xiaodu.acer.phoneplay.ui.activity.AppDetailActivity;
import com.xiaodu.acer.phoneplay.ui.adapter.AppInfoAdapter;
import com.xiaodu.acer.phoneplay.ui.decoration.DividerItemDecoration;

import javax.inject.Inject;

import butterknife.BindView;
import retrofit2.Retrofit;

/**
 * @author 小杜
 *         邮箱:m15701207383@163.com
 * @version V1.0
 * @Description: 所有的应用相关数据继承这个类
 * @date 2018/1/9 11:22
 */

public abstract class BaseAppInfoFragment extends ProgressFragment<AppInfoPresenter>
        implements AppInfoContract.AppInfoView, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.recycle_view)
    protected RecyclerView mRecyclerView;


    @Inject
    protected Retrofit mRetrofit;

    protected AppInfoAdapter mAdapter;

    protected int page = 0;


    @Override
    public void init() {

        mPresenter.requestData(type(), page);

        initRecyclerView();


    }


    protected void initRecyclerView() {


        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);

        mRecyclerView.addItemDecoration(itemDecoration);
        mAdapter = buildAdapter();

        mAdapter.setOnLoadMoreListener(this);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                AppInfo appInfo = mAdapter.getItem(position);
                mApplication.setView(view);
                Intent intent = new Intent(getActivity(), AppDetailActivity.class);
                intent.putExtra("appinfo", appInfo);
                startActivity(intent);
            }
        });


    }

    protected abstract int type();

    public abstract AppInfoAdapter buildAdapter();


    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerAppInfoComponent.builder().appComponent(appComponent)
                .appInfoModule(new AppInfoModule(this))
                .build().injectTopListFragment(this);

    }

    @Override
    public int setLayout() {
        return R.layout.template_recycler_view;
    }


    @Override
    public void showResult(PageBean<AppInfo> pageBean) {

        mAdapter.addData(pageBean.getDatas());

        if (pageBean.isHasMore()) {
            page++;
        }
        mAdapter.setEnableLoadMore(pageBean.isHasMore());
    }

    @Override
    public void onLoadMoreComplete() {
        mAdapter.loadMoreComplete();
    }

    @Override
    public void onLoadMoreRequested() {
        //子线程
        mPresenter.requestData(type(), page);
    }

    @Override
    public void onEmptyViewClick() {
        mPresenter.requestData(type(), page);
    }
}
